package cn.kgc.mes.storage.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.kgc.mes.storage.domain.StorageLocation;
import com.alibaba.fastjson2.JSON;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.kgc.mes.common.annotation.Log;
import cn.kgc.mes.common.core.controller.BaseController;
import cn.kgc.mes.common.core.domain.AjaxResult;
import cn.kgc.mes.common.enums.BusinessType;
import cn.kgc.mes.storage.domain.StorageArea;
import cn.kgc.mes.storage.service.IStorageAreaService;
import cn.kgc.mes.common.utils.poi.ExcelUtil;
import cn.kgc.mes.common.core.page.TableDataInfo;

/**
 * 库位设置Controller
 * 
 * @author ydw
 * @date 2025-09-12
 */
@RestController
@RequestMapping("/area/area")
public class StorageAreaController extends BaseController
{
    @Autowired
    private IStorageAreaService storageAreaService;

    /**
     * 查询库位设置列表
     */
//    @PreAuthorize("@ss.hasPermi('area:area:list')")
    @GetMapping("/list")
    public TableDataInfo list(StorageArea storageArea)
    {
        startPage();
        List<StorageArea> list = storageAreaService.selectStorageAreaList(storageArea);
        return getDataTable(list);
    }

    /**
     * 导出库位设置列表
     */
//    @PreAuthorize("@ss.hasPermi('area:area:export')")
    @Log(title = "库位设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StorageArea storageArea)
    {
        List<StorageArea> list = storageAreaService.selectStorageAreaList(storageArea);
        ExcelUtil<StorageArea> util = new ExcelUtil<StorageArea>(StorageArea.class);
        util.exportExcel(response, list, "库位设置数据");
    }

    /**
     * 获取库位设置详细信息
     */
//    @PreAuthorize("@ss.hasPermi('area:area:query')")
    @GetMapping(value = "/{areaId}")
    public AjaxResult getInfo(@PathVariable("areaId") Long areaId)
    {
        return success(storageAreaService.selectStorageAreaByAreaId(areaId));
    }

    /**
     * 新增库位设置
     */
//    @PreAuthorize("@ss.hasPermi('area:area:add')")
    @Log(title = "库位设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StorageArea storageArea)
    {
        return toAjax(storageAreaService.insertStorageArea(storageArea));
    }

    /**
     * 修改库位设置
     */
//    @PreAuthorize("@ss.hasPermi('area:area:edit')")
    @Log(title = "库位设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StorageArea storageArea)
    {
        return toAjax(storageAreaService.updateStorageArea(storageArea));
    }

    /**
     * 删除库位设置
     */
//    @PreAuthorize("@ss.hasPermi('area:area:remove')")
    @Log(title = "库位设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{areaIds}")
    public AjaxResult remove(@PathVariable Long[] areaIds)
    {
        return toAjax(storageAreaService.deleteStorageAreaByAreaIds(areaIds));
    }


    //修改库区是否启用
    @PostMapping("/updatenew")
    public Object updata1(@RequestBody StorageArea storagearea){
        boolean result=storageAreaService.updateStorageAreaIsEnable(storagearea);
        Map<String,Object> map=new HashMap<>();
        if(result){
            map.put("code",200);
            map.put("msg","修改成功");
        }else{
            map.put("code",500);
            map.put("msg","修改失败");
        }
        return JSON.toJSONString(map);
    }
}
